package com.security.demo.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import com.security.demo.entity.SysUser;
import com.security.demo.rep.SysUserRep;

/**
 * 使用用户名对用户进行验证。这里之所以要实现UserDetailsService接口，是因为在Spring Security中我们配置相关参数需要UserDetailsService类型的数据
 * @author cj
 *
 */
public class CustomUserService implements UserDetailsService{

	@Autowired
	private SysUserRep sysUserRep;
	
	/**
	 * 重写UserDetailsService的loadUserByUsername(String arg0)方法验证用户名称，这里只验证了用户名存不存在。
	 */
	@Override
	public UserDetails loadUserByUsername(String arg0) throws UsernameNotFoundException {
		SysUser user=sysUserRep.findByUsername(arg0);
		if(user==null){
			throw new UsernameNotFoundException("用户名不存在");
		}
		System.out.println("name:"+arg0);
		System.out.println("username:"+user.getUsername()+";password:"+user.getPassword());
		return user;
	}

}
